package com.tbit.uqbike.webmanager.util.manager.factory;


import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.uqbike.object.pojo.MoneyOpeLog;
import com.tbit.uqbike.object.pojo.RideLog;
import com.tbit.uqbike.object.pojo.RideOrderMoneyOpeLog;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.capital.MoneyOpeLogDao;
import com.tbit.uqbike.webmanager.dao.log.OperateLogDao;
import com.tbit.uqbike.webmanager.dao.log.RideOrderMoneyOpeLogDao;
import com.tbit.uqbike.webmanager.util.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author jing
 */
@Slf4j
public class AsyncFactory {
    /**
     * 操作日志记录
     *
     * @return 任务task
     */
    public static TimerTask recordOper(final LoginInfo loginUser, final OperateLog annotation, final Map<String, Object> nameValueMap,
                                       final Object obj, final String ip, final String requestURI) {
        return new TimerTask() {
            @Override
            public void run() {
                // 先打日志后面再入库
                log.info(String.format("[%s]:name:%s,phone:%s,userId:%s,type:%d,id:%d,module:%s,Method:%s,param:%s,ip:%s,obj:%s",
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())), loginUser.getName(), loginUser.getPhone(), loginUser.getAccountUserId(), loginUser.getType(), loginUser.getId(), annotation.module().getName(), annotation.methods().getName(),
                        JSONObject.toJSONString(nameValueMap), ip, JSONObject.toJSONString(obj)));
                //记录日志
                com.tbit.uqbike.object.pojo.OperateLog operateLog = new com.tbit.uqbike.object.pojo.OperateLog(loginUser.getType(), loginUser.getName(), loginUser.getPhone(), loginUser.getId(), new Date(), annotation.module().getName(), annotation.module().getSn(), annotation.methods().getName(), annotation.methods().getSn(),
                        ip, requestURI, JSONObject.toJSONString(nameValueMap));
                SpringUtils.getBean(OperateLogDao.class).insertOperateLog(operateLog);
            }
        };
    }


    /**
     * 修改用余额和礼品卡余额进行日志记录
     *
     * @return 任务task
     */
    public static TimerTask recordUserMoneyUpdate(final OperateLog annotation, final String[] params, final Object[] args, final LoginInfo loginUser) {
        return new TimerTask() {
            @Override
            public void run() {
                /**修改用余额和礼品卡余额进行日志记录*/
                if (annotation.methods() == MethodEnum.GIFTCARD_RECHANGE || annotation.methods() == MethodEnum.USER_EDITMONEY) {
                    Integer accountId;
                    Integer moneyType;
                    Integer money;
                    String operaRemark;
                    User user;
                    UserDao userDao = SpringUtils.getBean(UserDao.class);
                    if ("accountId".equals(params[0])) {
                        /**修改的是礼品卡*/
                        accountId = (Integer) args[0];
                        moneyType = 2;
                        money = (Integer) args[2];
                        operaRemark = (String) args[3];
                        user = userDao.getByUserId((Integer) args[1]);
                    } else {
                        /**修改用户余额*/
                        user = userDao.getByUserId((Integer) args[0]);
                        accountId = user.getAccountId();
                        moneyType = 1;
                        money = (Integer) args[1];
                        operaRemark = (String) args[2];
                    }
                    Integer changeType = money > 0 ? 1 : 0;
                    money = Math.abs(money);
                    SpringUtils.getBean(MoneyOpeLogDao.class).insert(new MoneyOpeLog(accountId, moneyType, changeType, money, new Date(), user.getUserId(), user.getPhone(), loginUser.getAccountUserId(), loginUser.getPhone(), operaRemark));
                }
            }
        };
    }


    public static TimerTask recordRideMoneyUpdate(OperateLog annotation, RideOrderMoneyOpeLog rideOrderMoneyOpeLog, RideLog ridelog) {
        return new TimerTask() {
            @Override
            public void run() {
                if (annotation.methods() == MethodEnum.ORDER_EDIT) {
                    /**记录骑行金额修改日志*/
                    if (null != rideOrderMoneyOpeLog) {
                        rideOrderMoneyOpeLog.setNewMoney(ridelog.getMoney());
                        rideOrderMoneyOpeLog.setOperaTime(new Date());
                        rideOrderMoneyOpeLog.setOperaRemark(ridelog.getRemark());
                        SpringUtils.getBean(RideOrderMoneyOpeLogDao.class).insert(rideOrderMoneyOpeLog);
                    }
                }
            }
        };
    }
}
